package com.tfdy.util;

/**
 * @ClassName AngleTool
 * @Description: TODO
 * @Author 周路省
 * @Date 2020/3/28
 * @Version V1.0
 **/
public class AngleTool {
    /**
     * 计算方位角
     *
     * @param x1 lat1
     * @param y1 lng1
     * @param x2 lat2
     * @param y2 lng2
     * @return
     */
    public static double azimuthAngle(double x1, double y1, double x2, double y2) {
        double dx, dy, angle = 0;
        dx = x2 - x1;
        dy = y2 - y1;
        if (x2 == x1) {
            angle = Math.PI / 2.0;
            if (y2 == y1) {
                angle = 0.0;
            } else if (y2 < y1) {
                angle = 3.0 * Math.PI / 2.0;
            }
        } else if ((x2 > x1) && (y2 > y1)) {
            angle = Math.atan(dx / dy);
        } else if ((x2 > x1) && (y2 < y1)) {
            angle = Math.PI / 2 + Math.atan(-dy / dx);
        } else if ((x2 < x1) && (y2 < y1)) {
            angle = Math.PI + Math.atan(dx / dy);
        } else if ((x2 < x1) && (y2 > y1)) {
            angle = 3.0 * Math.PI / 2.0 + Math.atan(dy / -dx);
        }

        return (angle * 180 / Math.PI);
    }
}
